const multer = require('koa-multer');
let uploadFolder = 'data/uploads/';

let storage = multer.diskStorage({
    //设置上传文件路径,以后可以扩展成上传至七牛,文件服务器等等
    //Note:如果你传递的是一个函数，你负责创建文件夹，如果你传递的是一个字符串，multer会自动创建
    destination: function (req, file, cb) {
        let type = file.mimetype;
        if (type === 'image/png' || type === 'image/jpeg' || type === 'image/jpg') {
            cb(null, uploadFolder + 'image/');//保存的路径，需要自己创建
        } else {
            cb(null, uploadFolder + 'file/');
        }
    },
    filename: function (req, file, cb) {
        let fileFormat = (file.originalname).split(".");
        cb(null, Date.now() + "." + fileFormat[fileFormat.length - 1]);
    }
});

//添加配置文件到multer对象
let upload = multer({
    storage: storage
});

module.exports = upload;